package cn.study.controller;


import cn.study.entity.*;
import cn.study.service.OssService;
import cn.study.service.StudyBannerService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.apache.catalina.filters.Constants;
import org.apache.velocity.shaded.commons.io.FilenameUtils;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;

/**
 * <p>
 * 轮播图表 前端控制器
 * </p>
 *
 * @author qiao
 * @since 2020-06-22
 */
@Controller
@Slf4j
@RequestMapping("/study-banner")
public class StudyBannerController {
    @Resource
    StudyBannerService studyBannerService;
    @Resource
    OssService ossService;
    @GetMapping("/list")
        public String list(Model model, @RequestParam(value="pn",defaultValue="1") Integer pn,
            @RequestParam(value="paginalsize",defaultValue = "5") Integer paginalsize)
        {
            PageHelper.startPage(pn, paginalsize);

            List<StudyBanner> list=studyBannerService.getList();
            PageInfo pInfo=new PageInfo(list,10);
            model.addAttribute("pInfo",pInfo);
            model.addAttribute("paginalsize",paginalsize);

            return "study_banner";
    }

    @GetMapping("del")
    public String del(Integer id)
    {
        boolean bl=studyBannerService.removeById(id);
        if(bl){
            log.info("删除成功");
        }else {
            log.info("删除失败");
        }
        return "redirect:/study-banner/list";
    }
    @GetMapping("/add")
    public String add()
    {

        return "study_banner_add";
    }

    @PostMapping("/banneradd")
    public String banneradd(StudyBanner studyBanner,@RequestParam("file") MultipartFile file){
        String url=ossService.uploadFileAvatar(file);
        studyBanner.setBannerPath(url);

        boolean bl=studyBannerService.save(studyBanner);


        if(bl){
            log.info("添加成功");
        }else {
            log.info("添加失败");
        }
        return "redirect:/study-banner/list";
    }
    @GetMapping("/upd")
    public String upd(Integer id,Model model)
    {
        StudyBanner studyBanner=studyBannerService.getById(id);
        model.addAttribute("list",studyBanner);


        return "study_banner_upd";
    }

    @PostMapping("/bannerupd")
    public String bannerupd(Model model,StudyBanner studyBanner,@RequestParam("file") MultipartFile file)
    {String url=ossService.uploadFileAvatar(file);
        studyBanner.setBannerPath(url);
        studyBannerService.updateById(studyBanner);

        return "redirect:/study-banner/list";
    }
}

